package com.dengyafei.controller;

import com.dengyafei.annotation.JwtToken;
import com.dengyafei.bean.AppointmentEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.constant.ResultMessage;
import com.dengyafei.domin.vo.AppointmentAddVO;
import com.dengyafei.domin.vo.AppointmentCancelVO;
import com.dengyafei.domin.vo.AppointmentSearchVO;
import com.dengyafei.domin.vo.AppointmentSendVO;
import com.dengyafei.result.RestApiResult;
import com.dengyafei.result.Result;
import com.dengyafei.service.AppointmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 邓亚非
 */
@Slf4j
@RequestMapping(value = "/appointment")
@RestController
public class AppointmentController {

    @Autowired
    AppointmentService appointmentService;

    /**
     * 添加预约
     *
     * @param appointmentAddVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/add")
    public RestApiResult add(@RequestBody AppointmentAddVO appointmentAddVO) {
        log.info("【预约模块-添加预约接口-参数】appointmentAddVO:[{}]", appointmentAddVO);
        Integer integer = appointmentService.insertOne(appointmentAddVO);
        if (integer.equals(RecommendConstant.ALREADY_APPOINTMENT)) {
            log.error("【预约模块-添加预约接口-失败】appointmentAddVO:[{}]", ResultMessage.Appointment.ALREADY_APPOINTMENT);
            return RestApiResult.error(ResultMessage.Appointment.ALREADY_APPOINTMENT);
        }
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            log.error("【预约模块-添加预约接口-失败】appointmentAddVO,cause:[{}]", ResultMessage.Appointment.APPOINTMENT_FAILURE);
            return RestApiResult.error(ResultMessage.Appointment.APPOINTMENT_FAILURE);
        }
        log.info("【预约模块-添加预约接口-成功】appointmentAddVO:[{}]", ResultMessage.Appointment.APPOINTMENT_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Appointment.APPOINTMENT_SUCCESS);
    }

    /**
     * 查询预约
     *
     * @return
     */
    @JwtToken
    @PostMapping(value = "/load")
    public RestApiResult load() {
        log.info("【预约模块-企业用户查询预约接口-参数】:[{}]", "无参");
        List<AppointmentEntity> appointmentEntities = appointmentService.queryByHid();
        log.info("【预约模块-企业用户查询预约接口-成功】:[{}]", ResultMessage.Appointment.APPOINTMENT_QUERY_SUCCESS);
        return RestApiResult.ok(appointmentEntities);
    }

    /**
     * 企业用户条件搜索
     *
     * @param appointmentSearchVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/search")
    public RestApiResult search(@RequestBody AppointmentSearchVO appointmentSearchVO) {
        log.info("【预约模块-企业用户搜索接口-参数】[{}]", appointmentSearchVO);
        List<AppointmentEntity> appointmentEntities = appointmentService.queryByHidAndParams(appointmentSearchVO);
        log.info("【预约模块-企业用户搜索接口-成功】:[{}]", ResultMessage.Appointment.APPOINTMENT_QUERY_SUCCESS);
        return RestApiResult.ok(appointmentEntities);
    }

    /**
     * 发送通知邮件
     *
     * @param appointmentSendVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/send")
    public RestApiResult send(@RequestBody AppointmentSendVO appointmentSendVO) {
        log.info("【预约模块-发送通知接口-参数】:[{}]", appointmentSendVO);
        Boolean send = appointmentService.send(appointmentSendVO);
        if (send.equals(false)) {
            log.error("【预约模块-发送通知接口-失败】:[{}]", ResultMessage.Appointment.SEND_FAILURE);
            return RestApiResult.error(ResultMessage.Appointment.SEND_FAILURE);
        }
        log.info("【预约模块-发送通知接口-成功】:[{}]", ResultMessage.Appointment.SEND_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Appointment.SEND_SUCCESS);
    }

    /**
     * 应届生查询预约
     * @return
     */
    @JwtToken
    @PostMapping(value = "/queryByGid")
    public RestApiResult queryByGid(){
        log.info("【预约模块-应届生查询预约接口-参数】:[{}]","无参");
        List<AppointmentEntity> appointmentEntities = appointmentService.queryByGid();
        log.info("【预约模块-应届生查询预约接口-成功】:[{}]",ResultMessage.Appointment.APPOINTMENT_QUERY_SUCCESS);
        return RestApiResult.ok(appointmentEntities);
    }

    /**
     * 取消预约
     * @param appointmentCancelVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/cancel")
    public RestApiResult cancel(@RequestBody AppointmentCancelVO appointmentCancelVO){
        log.info("【预约模块-取消预约接口-参数】:[{}]",appointmentCancelVO);
        Integer cancel = appointmentService.cancel(appointmentCancelVO);
        if(!cancel.equals(RecommendConstant.OPERATION_SUCCESS)){
            log.error("【预约模块-取消预约接口-失败】:,cause:[{}]",ResultMessage.Appointment.APPOINTMENT_CANCEL_FAILURE);
            return RestApiResult.error(ResultMessage.Appointment.APPOINTMENT_CANCEL_FAILURE);
        }
        log.info("【预约模块-取消预约接口-成功】:[{}]", ResultMessage.Appointment.APPOINTMENT_CANCEL_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.Appointment.APPOINTMENT_CANCEL_SUCCESS);
    }
}
